package cn.swenty.backstage.base.web.system.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.swenty.backstage.base.web.system.entity.SysDepFormMap;
import cn.swenty.backstage.base.web.system.entity.SysDeptPhoneFormMap;
import cn.swenty.backstage.base.web.system.entity.SysPhoneSettingMap;
import cn.swenty.backstage.base.web.system.mapper.DepartMapper;
import cn.swenty.backstage.base.web.system.mapper.PhoneSettingMapper;
import cn.swenty.backstage.base.web.system.model.Dept;
import cn.swenty.backstage.framework.common.util.Bean2MapUtils;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.common.util.TreeUtil;
import cn.swenty.backstage.framework.service.BaseService;
import cn.swenty.backstage.framework.util.IdGenerator;

/**
 * 部门管理
 * @author LiuHao
 *
 */

@Service
public class DepartService extends BaseService {
	@Inject
	private  DepartMapper depMapper;
	
	@Inject
	private PhoneSettingMapper phoneSettingMapper;
	
	
	/**
	 * 通过deptid和父类级别查找父级别部门
	 * @param deptId
	 * @param level
	 * @return
	 */
	public SysDepFormMap findUpDept(String deptId,int level){
		List<SysDepFormMap> depFormMaps = findUpDept(null, deptId);
		if(level>depFormMaps.size()){
			return null;
		}else{
			return depFormMaps.get(level-1);
		}
	}
	
	public List<SysDepFormMap> findUpDept(List<SysDepFormMap> depFormMaps, String deptId){
		if(depFormMaps == null) {depFormMaps = new ArrayList<SysDepFormMap>();}
		SysDepFormMap depFormMap = new SysDepFormMap();
		depFormMap.put("dept_id", deptId);
		depFormMap = depMapper.findById(depFormMap);
		if(depFormMap!=null){
			findUpDept(depFormMaps, depFormMap.getStr("parent_id"));
			depFormMaps.add(depFormMap);
		}
		return depFormMaps;
	}
	
	public void saveDept(SysDepFormMap deptFormMap,String setting)throws RuntimeException{
		try {
			this.save(deptFormMap);
			if(StringUtil.isNotNull(setting)){
				SysDeptPhoneFormMap deptPhoneFormMap = new SysDeptPhoneFormMap();
				deptPhoneFormMap.put("setting_id", setting);
				deptPhoneFormMap.put("id", IdGenerator.getNextId());
				deptPhoneFormMap.put("dept_id", deptFormMap.getStr("dept_id"));
				this.save(deptPhoneFormMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	
	public void updateDept(SysDepFormMap deptFormMap,String setting)throws RuntimeException{
		try {
			this.updateFull(deptFormMap);
			SysDeptPhoneFormMap deptPhoneFormMap = new SysDeptPhoneFormMap();
			deptPhoneFormMap.put("dept_id", deptFormMap.getStr("dept_id"));
			this.delete(deptPhoneFormMap);
			if(StringUtil.isNotNull(setting)){
				deptPhoneFormMap.put("dept_id", deptFormMap.getStr("dept_id"));
				deptPhoneFormMap.put("id", IdGenerator.getNextId());
				deptPhoneFormMap.put("setting_id", setting);
				this.save(deptPhoneFormMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	
	
	/**
	 * 查询树状的部门表
	 * @return
	 */
	public List<Dept> findDeptTree(){
		
		try {
			List<SysDepFormMap> depts = this.queryAll(SysDepFormMap.class);
			List<Dept> ds = Bean2MapUtils.maps2Objects(depts, Dept.class);
			Collections.sort(ds);
			return TreeUtil.createTree(ds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<SysDepFormMap> find(SysDepFormMap sdf){
		List<SysDepFormMap> list = super.findByPage(sdf);
		for(SysDepFormMap depFormMap:list){
			SysPhoneSettingMap map = new SysPhoneSettingMap();
			map.put("dept_id", depFormMap.getStr("dept_id"));
			List<SysPhoneSettingMap> phoneSettingMaps = phoneSettingMapper.findByDept(map);
			if(phoneSettingMaps!=null && phoneSettingMaps.size()>0){
				depFormMap.put("setting_id", phoneSettingMaps.get(0).getStr("setting_id"));
				depFormMap.put("setting_name", phoneSettingMaps.get(0).getStr("setting_name"));
			}
		}
		return list;
	}
	
	
	/**
	 * 编辑状态
	 * @param ids
	 */
	public void updateStatus(SysDepFormMap deptFormMap){
			
			this.updateField(SysDepFormMap.class,deptFormMap);
	}
	
	
	
	/**
	 * 按照id删除部门，删除该部门需要将子集的部门也删除
	 * 
	 * @param ids
	 * @throws Exception 
	 */
	@Transactional
	public void deleteByIds(String[] ids) throws Exception{
		for(int i=0;i<ids.length;i++){
			dropChildren(ids[i]);
		}
	}
	
	/**
	 * 递归删除子集
	 * 
	 * @param id
	 * @throws Exception 
	 */
	public void dropChildren(String id) throws Exception{
		SysDepFormMap deptFormMap = new SysDepFormMap();
		deptFormMap.put("parent_id", id);
		List<SysDepFormMap> deptFormMaps = depMapper.findByParent(deptFormMap);
		for(int i=0;i<deptFormMaps.size();i++){
			dropChildren(deptFormMaps.get(i).getStr("dept_id"));
		}
		
		this.deleteById(SysDepFormMap.class, id);
		SysDeptPhoneFormMap deptPhoneFormMap = new SysDeptPhoneFormMap();
		deptPhoneFormMap.put("dept_id", id);
		this.delete(deptPhoneFormMap);

	}
	
	
	/**
	 * 以逗号分隔
	 * @param deptFormMap
	 * @return
	 */
	public String findHiddens(){
		String list="";
		//获取隐藏的部门信息
		SysDepFormMap sysDepFormMap= new SysDepFormMap();
		sysDepFormMap.put("dept_state", 1);
		List<SysDepFormMap> hiddens=  depMapper.findHiddens(sysDepFormMap);
		for (SysDepFormMap string : hiddens) {
			if(string.get("dept_id")!=null){
				list +=string.get("dept_id")+",";
			}
		}
		return list;
	}
	
	public List<SysDepFormMap> findDepPage(SysDepFormMap deptFormMap){
		return depMapper.findDepPage(deptFormMap);
	}
	
	public List<SysDepFormMap> depNode(SysDepFormMap deptFormMap){
		return depMapper.depNode(deptFormMap);
	}
	
	

	

	
}
